package com.jumi.microservice.controller.usercash.admin;

import com.alibaba.excel.EasyExcel;
import com.jumi.microservice.dto.excel.ExportExcelCommonResponse;
import com.jumi.microservice.dto.userWallet.ExportCapital;
import com.jumi.microservice.dto.userWallet.ExportCapitalRequest;
import com.jumi.microservice.service.usercash.admin.ExportWalletService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@Api(tags = "导出")
@RequestMapping("/export")
public class ExportController {
    @Resource
    ExportWalletService exportWalletService;

    @ApiOperation("导出资金明细")
    @PostMapping("/exportWalletService")
    public void exportWalletService(HttpServletResponse httpServerResponse, ExportCapitalRequest request) throws IOException {
        request.checkRequest();
        if(request.getEndTime()==null && request.getMembershipLevel()==null && (request.getPhone()==null || request.getPhone().trim().isEmpty()) &&
                request.getStartTime()==null && request.getType()==null){
            return;
        }
        ExportExcelCommonResponse<ExportCapital> responseExportExcelCommonResponse = exportWalletService.exportExcelCommonResponse(request);
        httpServerResponse.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
        httpServerResponse.setCharacterEncoding("utf-8");
        httpServerResponse.setHeader("Content-disposition", "attachment;filename=" + responseExportExcelCommonResponse.getFileName());
        EasyExcel.write(httpServerResponse.getOutputStream(), ExportCapital.class).sheet("sheet1").doWrite(responseExportExcelCommonResponse.getData());
    }
}
